package cn.dida.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dida.dao.BranchDao;
import cn.dida.dao.CompanyDao;
import cn.dida.exception.BizzException;
import cn.dida.model.Branch;
import cn.dida.model.Company;
import cn.dida.model.Pageable;
import cn.dida.service.BranchService;
import cn.dida.weixin.qyh.ErrorCodeMapping;
import cn.dida.weixin.qyh.QyhApiClient;

import com.alibaba.fastjson.JSONObject;
/**
 * 网点管理 业务层接口实现类
 * @author weiwei
 * */
@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchDao branchDao;
    @Autowired
    private CompanyDao companyDao;

    @Override
    public Pageable<Branch> queryBranch(Integer companyId, String keyword, Integer state, int page) {
        return branchDao.query(companyId, keyword, state, page);
    }

    @Override
    public List<Branch> queryBranch(Integer companyId, Integer state) {
        return branchDao.query(companyId, state);
    }

    @Transactional
    @Override
    public Boolean insertBranch(Branch branch) {

        //录入微信企业号通讯录
        Company company = companyDao.get(branch.getCompanyId());
        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String corpid = preference.getString("weixin_qyh_corpid");
        String corpsecret = preference.getString("weixin_qyh_corpsecret");
        QyhApiClient qyhApiClient = new QyhApiClient(corpid, corpsecret);

        JSONObject re = qyhApiClient.createUser(branch.getLinkPhone(), branch.getName(), branch.getDepartmentId(), branch.getLinkPhone());
        if (re.getInteger("errcode") == 0) {
            return branchDao.insert(branch) == 1;
        } else {
            throw new BizzException(BizzException.error_code_invalid_request, ErrorCodeMapping.getMsgByCode(re.getInteger("errcode")));
        }
    }

    @Transactional
    @Override
    public Boolean updateBranch(Branch branch) {
        //更新微信企业号通讯录
        Company company = companyDao.get(branch.getCompanyId());

        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String corpid = preference.getString("weixin_qyh_corpid");
        String corpsecret = preference.getString("weixin_qyh_corpsecret");
        QyhApiClient qyhApiClient = new QyhApiClient(corpid, corpsecret);

        JSONObject re = qyhApiClient.updateUser(branch.getLinkPhone(), branch.getName(), branch.getDepartmentId(), branch.getLinkPhone());
        if (re.getInteger("errcode") == 0) {
            return branchDao.update(branch) == 1;
        } else {
            throw new BizzException(BizzException.error_code_invalid_request, ErrorCodeMapping.getMsgByCode(re.getInteger("errcode")));
        }

    }

    @Override
    public Branch getBranch(Integer companyId, Integer id) {
        return branchDao.get(companyId, id);
    }

    @Override
    public Branch getByLinkPhone(Integer companyId, String linkPhone) {
        return branchDao.getByLinkPhone(companyId, linkPhone);
    }
    @Transactional
    @Override
    public Boolean deleteBranch(Integer companyId, Integer id) {
        //删除微信企业号通讯录
        Company company = companyDao.get(companyId);
        JSONObject preference = JSONObject.parseObject(company.getPreference());
        String corpid = preference.getString("weixin_qyh_corpid");
        String corpsecret = preference.getString("weixin_qyh_corpsecret");
        QyhApiClient qyhApiClient = new QyhApiClient(corpid, corpsecret);

        Branch branch = branchDao.get(companyId, id);
        if (branch == null) {
            return true;
        }

        JSONObject re = qyhApiClient.deleteUser(branch.getLinkPhone());
        if (re.getInteger("errcode") == 0) {
            return branchDao.delete(companyId, id) == 1;
        } else {
            throw new BizzException(BizzException.error_code_invalid_request, ErrorCodeMapping.getMsgByCode(re.getInteger("errcode")));
        }
    }

    @Override
    public Boolean delPBByBranchId(String branchId) {
        return branchDao.delPBByBranchId(branchId) == 1;
    }

    @Override
    public List<Branch> queryBranchForExport(Integer companyId, String keyword,
                                             Integer state) {
        // TODO Auto-generated method stub
        return branchDao.queryForExport(companyId, keyword, state);
    }
}
